package net.xiaoyu233.mitemod.miteite.inventory.container;

import net.minecraft.*;
import net.xiaoyu233.mitemod.miteite.item.recipe.FusionmineRecipe;
import net.xiaoyu233.mitemod.miteite.item.recipe.FusionmineRecipes;
import net.xiaoyu233.mitemod.miteite.tileentity.TileEntityFusionmine;

import java.util.ArrayList;

public class ContainerFusionmine extends Container {

    private TileEntityFusionmine fusionmine;


    public ContainerFusionmine(EntityPlayer player, TileEntityFusionmine tileEntityFusionmine) {
        super(player);
        this.fusionmine = tileEntityFusionmine;

        this.addSlotToContainer(new SoltFusionmine(this, tileEntityFusionmine, 0, 16, 37));
        this.addSlotToContainer(new SoltFusionmine(this, tileEntityFusionmine, 1, 45, 37));
        this.addSlotToContainer(new SoltFusionmine(this, tileEntityFusionmine, 2, 74, 37));
        this.addSlotToContainer(new SoltFusionmine(this, tileEntityFusionmine, 3, 45, 58));
        this.addSlotToContainer(new SoltFusionmine(this, tileEntityFusionmine, 4, 142, 37));

        int col;
        for (int row = 0; row < 3; ++row) {
            for (col = 0; col < 9; ++col) {
                this.addSlotToContainer(new Slot(player.inventory, col + row * 9 + 9, 8 + col * 18, 84 + row * 18));
            }
        }

        // 玩家快捷栏
        for (col = 0; col < 9; ++col) {
            this.addSlotToContainer(new Slot(player.inventory, col, 8 + col * 18, 142));
        }
    }

    @Override
    public boolean canInteractWith(EntityPlayer entityPlayer) {
        return this.fusionmine.isUseableByPlayer(entityPlayer);
    }

    @Override
    public void onContainerClosed(EntityPlayer par1EntityPlayer) {
        super.onContainerClosed(par1EntityPlayer);
        this.fusionmine.isUsing = false;
        if (!this.world.isRemote) {
            this.updatePlayerInventory(par1EntityPlayer);
//             this.fusionmine.isUsing = false;
//             this.fusionmine.closeChest();
            //        是否掉落

            for (int i = 0; i < 4; ++i) {
                ItemStack var2 = this.fusionmine.getStackInSlotOnClosing(i);
                if (var2 != null) {
                    par1EntityPlayer.dropPlayerItem(var2);
                    this.fusionmine.closeChest();
                }
            }
            this.fusionmine.destroyInventory();
        }

    }

    public void updatePlayerInventory(EntityPlayer player) {
        ArrayList<ItemStack> itemList = new ArrayList();

        for (int index = 0; index < player.openContainer.inventorySlots.size(); ++index) {
            itemList.add(((Slot) player.openContainer.inventorySlots.get(index)).getStack());
        }

        ((ServerPlayer) player).updateCraftingInventory(player.openContainer, itemList);
    }

    public ItemStack transferStackInSlot(EntityPlayer par1EntityPlayer, int par2) {
        if (this.fusionmine.getInsynthesis() && par2 < 5) {
            return null; // 直接返回，不允许移动
        }
        ItemStack var3 = null;
        Slot var4 = (Slot) this.inventorySlots.get(par2);

        if (var4 != null && var4.getHasStack()) {
            ItemStack var5 = var4.getStack();
            var3 = var5.copy();

            // 1. 如果操作的是合成槽（0-4）
            if (par2 < 5) {
                // 从合成槽移动到背包
                if (!this.mergeItemStack(var5, 5, 41, true)) { // 5-41 是玩家背包和快捷栏
                    return null;
                }
                var4.onSlotChange(var5, var3);
            }
            // 2. 如果操作的是玩家背包或快捷栏（5-41）
            else {
                // 检查物品是否是煤炭块
                boolean isCoalBlock = var5.getItem() == Item.getItem(Block.coalBlock) || var5.getItem() == Item.coal;

                // 如果目标是煤炭块槽（索引3）
                if (isCoalBlock) {
                    // 尝试将煤炭块移动到槽3
                    if (!this.mergeItemStack(var5, 3, 4, false)) {
                        return null;
                    }
                }
                // 如果目标是非煤炭块槽（0,1,2）
                else {
                    // 检查是否是合成材料（根据配方）
                    boolean isInputItem = false;
                    for (FusionmineRecipe recipe : FusionmineRecipes.getAllRecipes()) {
                        if (recipe.matchesInput(var5)) { // 假设添加了 matchesInput 方法检查物品是否匹配输入
                            isInputItem = true;
                            break;
                        }
                    }
                    // 移动到输入槽（0,1,3）
                    if (isInputItem) {
                        if (!this.mergeItemStack(var5, 0, 3, false)) {
                            return null;
                        }
                    }
                }
            }

            // 处理物品数量
            if (var5.stackSize == 0) {
                var4.putStack(null);
            } else {
                var4.onSlotChanged();
            }

            if (var5.stackSize == var3.stackSize) {
                return null;
            }

            var4.onPickupFromSlot(par1EntityPlayer, var5);
        }

        return var3;
    }

    public TileEntityFusionmine getTileEntityFurnace() {
        return this.fusionmine;
    }
}
